Skip to content

Stop streamed text at the earliest matching sequence - #1769

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/earliest-stop-sequence
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/earliest-stop-sequence

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

With stops ordered as ['<LATE>', '<EARLY>'], a text delta containing answer<EARLY>hidden<LATE>tail currently emits answer<EARLY>hidden. The handler returns on the first configured sequence it finds instead of the first match in the text. Splitting the same response just after <EARLY> instead emits only answer.

The SDK uses this handler in promptAiSdkStream. Emitting a configured stop marker and the following text violates the stop contract that returned text excludes the stop sequence (API reference).

Changes

Select the smallest complete-match index in the buffered text before entering the finished state. Keep the existing partial-sequence buffering, flush behavior, and handler API.

Add one regression using two configuration orders and every two-chunk split of the same response. It checks the output, completion state, and suppression of later text.

Verification

  • On unchanged main c21711a, the new regression fails; the two affected test files report 19 passed / 1 failed. With the fix: 20 passed / 0 failed, 327 assertions.
  • An injected-fetch SSE fixture through the actual SDK provider converter and promptAiSdkStream also fails on main. With the fix, both combined and split deltas emit only answer, make one request each, and yield no error chunks.
  • Common and SDK typechecks pass with the locked native TypeScript 7.0.2 compiler; frozen-lockfile install passes.
  • ESLint: 0 errors, 4 local resolver warnings. The new test is formatted; the source file's existing Prettier differences are unchanged.
  • Raw SDK build fails on both main and this branch: the lock assigns TypeScript 7.0.2 to dts-bundle-generator, whose import needs ts.sys. Raw binary build fails through the same SDK step.
  • Supplemental SDK declaration/binary builds, version, and smoke pass when only the local declaration-builder dependency is temporarily mapped to the already installed JavaScript TypeScript 5.5.4. The original dependency directory was restored afterward. These are supplemental results, not a pass of the unmodified dependency build.

Notes / Scope

This changes text filtering when multiple complete stops are available in one buffer. Existing overlap/prefix buffering semantics stay as they are. The patch contains two common-package files and no dependency, compiler, cancellation, retry, or tool-execution changes.

Validation ran on Windows with Bun 1.3.14 and local fixtures. Linux/macOS, live providers, and the full monorepo suite were not run. The independent declaration-build dependency problem remains for the upstream export to resolve.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and a clean, minimal fix. The bug is real: iterating stopSequences in configured order and returning on the first indexOf hit means a later-configured-but-earlier-occurring stop sequence gets missed, which indeed violates the stop contract (text after the earliest stop marker should never leak out). The fix in stop-sequence.ts correctly scans all stop sequences, tracks the minimum index, and only finalizes after checking them all — that's the right approach and matches how this class of 'first match in text' logic should work.

The added test in stop-sequence.test.ts is thorough: it checks both configuration orders and every two-chunk split point of the sample text, which is a sensible way to exercise the partial-buffer logic alongside the new earliest-match logic. That's more rigor than most PRs of this size bring.

A few things worth tightening before merge:

  • The loop still does a fresh this.buffer.indexOf(stopSequence) per stop sequence per chunk, which is fine for correctness but O(n*m) on large buffers with many stop sequences — not a blocker, just worth a comment if intentional.
  • Worth adding a test case where one stop sequence is a prefix of another (e.g. <A> and <AB>) combined with the earliest-index logic, since that's the trickiest interaction with the existing overlap-buffering code that wasn't touched.
  • The PR body's extensive verification notes about unrelated build/toolchain issues (dts-bundle-generator, raw SDK build) are good diligence but not relevant to reviewing this change itself; that noise could be trimmed in future PRs.

Overall: correct root-cause fix, in-scope files only, test included. Worth porting.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants